<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/12
 * Time: 15:05
 */

namespace app\index\controller;


use think\Controller;
use think\Request;
use think\Db;

class Lipei extends Controller
{
    /*
     * 创建理赔数据
     */
    public function create()
    {
        try {
            $data = Request::instance()->param();
            $username = Db::name('member')->where('id', $data['member_id'])->value('username');
            $find = (new \app\index\model\Lipei())->where('member_id', $data['member_id'])->find();
            if (!empty($find)) return ['code' => 400, 'message' => '已经添加过理赔数据'];
            $data['username'] = $username;
            $data['create_time'] = date('Y-m-d H:i:s', time());
            $data['register_time'] = $this->date($data['register_time']);
            $data['insurance_expiration_time'] = $this->date($data['insurance_expiration_time']);

            $ret = (new \app\index\model\Lipei())->allowField(true)->save($data);
            if ($ret) {
                return ['code' => 200, 'message' => '添加成功'];
            }
            return ['code' => 400, 'message' => '已经理赔过'];
        } catch (\Exception $e) {
            return ['code' => $e->getCode(),'message' => $e->getMessage()];
        }
    }
    protected function date($variable)
    {
        $year  = mb_substr($variable, 0, 4);
        $month = mb_substr($variable, 5, 2);
        $day   = mb_substr($variable, 8, 2);
        $date_create = date_create($year . '-' . $month . '-' . $day);
        $date = date_format($date_create,'Y-m-d');
        return $date;
    }

    /**
     * @param $member_id
     * @return array
     * 是否理赔过
     */
    public function is_create($member_id)
    {
        $find = (new \app\console\model\Lipei())->where('member_id', $member_id)->find();
        if (!empty($find)) {
            return ['code' => 400, 'message' => '已经添加过理赔数据'];
        }
        return ['code' => 200, 'message' => '没有理赔过可以添加'];
    }

    /**
     * @param $member_id
     * @return array理赔时间轴
     */
    public function index($member_id)
    {
        $data = Db::name('lipei')
            ->alias('l')
            ->join('lipei_process lp', 'l.id = lp.lipei_id')
//            ->join('lipei_type lt', 'lp.lipei_type_id = lt.id')
            ->field('lp.id,lp.create_time,lipei_type_id title')
            ->where('l.member_id',$member_id)
            ->select();
        return ['code' => 200, 'message' => '理赔流程数据', 'data' => $data ];
    }
}